import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.Subscription;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

import com.sun.org.apache.bcel.internal.generic.NEWARRAY;

/*
 * N. Dumont, TAI Lab
 *
 * Class Clear created on 19 mai 2011
 *
 * Copyright (c) 2009 Thales Communications
 * 160 bd de Valmy - BP82 92704 COLOMBES cedex - France
 * All rights reserved.
 *
 */

/**
 * @author tai
 *
 */
public class Clear {
	
	public static void main(String[] args) {
		Clear c= new Clear();
		c.start();
	}
	public void start() {
		
		String[] users = new String[]{
			"phidget",
			"nicolas",
			"mario",
			"gateway",
			"main",
			"test-server"
		};
		
		for(int i=0;i<users.length;i++){
			deleteNodesforUser(users[i]);
		}
	}
	
	private void deleteNode(PubSubManager manager , String nodeName){
		try {
			manager.deleteNode(nodeName);
			System.out.println(nodeName + " deleted.");
		} catch (XMPPException e) {
			System.out.println(nodeName + " not deleted:"+e.getMessage());
//			e.printStackTrace();
		}
	}
	
	private void deleteNodesforUser(String user){
		System.out.println("Deleting nodes of "+user);
		ConnectionConfiguration config = new ConnectionConfiguration("192.168.20.12", 5222);
		XMPPConnection connection = new XMPPConnection(config);
		try {
			connection.connect();
			connection.login(user, user);
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// PubSubManager creation
		PubSubManager manager = new PubSubManager(connection);
		
		
		deleteNode(manager,"TestNode");	
		deleteNode(manager,"org/osgi/framework/ServiceEvent/MODIFIED");
		deleteNode(manager,"org/osgi/framework/ServiceEvent/REGISTERED");
		deleteNode(manager,"org/osgi/framework/BundleEvent/RESOLVED");
		deleteNode(manager,"diyse/devices/70160/yChanged");
		deleteNode(manager,"org/osgi/service/log/LogEntry/LOG_DEBUG");
		deleteNode(manager,"org/osgi/service/log/LogEntry/LOG_INFO");
		deleteNode(manager,"diyse/devices/70160/xChanged");
		deleteNode(manager,"diyse/devices/70160/distanceChanged");
		deleteNode(manager,"org/osgi/service/log/LogEntry/LOG_ERROR");
		deleteNode(manager,"org/osgi/framework/BundleEvent/INSTALLED");
		deleteNode(manager,"org/osgi/framework/BundleEvent/STARTED");
		deleteNode(manager,"org/osgi/framework/ServiceEvent/UNREGISTERING");
		
		deleteNode(manager,"diyse/devices/52861/xChanged");
		deleteNode(manager,"diyse/devices/52861/yChanged");
		deleteNode(manager,"diyse/devices/52861/zChanged");
		
		connection.disconnect();
		System.out.println();
		System.out.println();
	}
}
